package jp.aist.gtrc.plus.scheduler.node;


import java.util.Collection;

import jp.aist.gtrc.plus.scheduler.util.Checker;


/*
 * If the node has at least the required resource, reserve as maximum as 
 * possible resource on the node.
 */
public class WholeResourceNodeChecker extends Checker<NodeInfo>{

	private final NodeAllocateRequest allocReq;

	public WholeResourceNodeChecker(NodeAllocateRequest allocReq) {

		this.allocReq = allocReq;

	}

	public boolean isOK(NodeInfo node){

                // for each node_spec defined in qsub -l nodes=node_spec[+node_spec..]
		for (NodeAllocateSet allocSet : allocReq.getRequestedNodeAllocateSets()) {
			if (allocSet.isAllocatedEnough()) {
				continue;
			}
			Collection<NodeResource> reqRsc = allocSet.getRequestedResources();
			NodeResource enoughRsc = node.getResource().getEnoughResource(node.getStatus(),reqRsc);
			if (enoughRsc != null) {
                                // allocSet is changed for the next filter iteration
				allocSet.addAllocatedNode(node);
                                int remainingSlotNum = node.getResource().getNodeCPUNum() - node.getStatus().getRunningJobNum();

//				node.setLaunceProcessNum(enoughRsc.getProcessNum());
				node.setLaunceProcessNum(remainingSlotNum);
System.out.println("Node " + node.getName() + " has been maximal allocated");
				return true;
			}
		}

		return false;

	}

}
